Institutional Pivot Matrix Code: 

declare upper;
# --- 1) Inputs ---
input barsBack = 280;
input lockLevels = yes;
input extendFullChart = yes;
input showZones = yes;
input showTargets = yes;
input showTimeTurns = yes;
input showLabels = yes;
# Zone retracement (50% - 62%)
input zoneTopPct = 0.50;
input zoneBotPct = 0.62;
# --- 78.6% Retracement Line ---
input show78Line = yes;
input retrace78Pct = 0.786;  # 78.6%
# Time Turns as % (multiples) of barsBack
input pct45  = 0.45;
input pct90  = 0.90;
input pct180 = 1.80;
# --- NEW: Gann PRICE Levels (45/90/180/360) ---
input showGannPrice = yes;
input showGannPriceLabels = yes;
input g45mult  = 0.25;   # 45-degree price pivot = 0.25x range
input g90mult  = 0.50;   # 90-degree price pivot = 0.50x range
input g180mult = 1.00;   # 180-degree price pivot = 1.00x range
input g360mult = 2.00;   # 360-degree price pivot = 2.00x range
def bn = BarNumber();
def lastBar = HighestAll(if !IsNaN(close) then bn else Double.NaN);
def scanStart = lastBar - barsBack;
# --- 2) The "Smart Window" Anchor Logic ---
def winHigh_raw = HighestAll(if bn >= scanStart then high else Double.NaN);
def winLow_raw  = LowestAll(if bn >= scanStart then low else Double.NaN);
# Lock the window extremes if desired
def winHigh = if lockLevels then HighestAll(winHigh_raw) else winHigh_raw;
def winLow  = if lockLevels then HighestAll(winLow_raw)  else winLow_raw;
def hiBar = HighestAll(if high == winHigh and bn >= scanStart then bn else 0);
def loBar = HighestAll(if low == winLow and bn >= scanStart then bn else 0);
# Logic: High before Low = Bearish | Low before High = Bullish
def isBullish = loBar < hiBar;
def rng = winHigh - winLow;
# --- 3) Directional Zone & Target Math ---
def bz1 = if isBullish then winHigh - (rng * zoneTopPct) else winLow + (rng * zoneTopPct);
def bz2 = if isBullish then winHigh - (rng * zoneBotPct) else winLow + (rng * zoneBotPct);
def zoneHi = Max(bz1, bz2);
def zoneLo = Min(bz1, bz2);
def trigger = if isBullish then zoneLo else zoneHi;
# Blue Target Hierarchy Math
def T62_val  = if isBullish then trigger + (rng * 0.618) else trigger - (rng * 0.618);
def T78_val  = if isBullish then trigger + (rng * 0.786) else trigger - (rng * 0.786);
def T100_val = if isBullish then trigger + (rng * 1.000) else trigger - (rng * 1.000);
# --- 4) Visual Plots & Stable Color Clouds ---
plot BullZoneHi = if isBullish and showZones and bn >= scanStart then HighestAll(zoneHi) else Double.NaN;
plot BullZoneLo = if isBullish and showZones and bn >= scanStart then HighestAll(zoneLo) else Double.NaN;
plot BearZoneHi = if !isBullish and showZones and bn >= scanStart then HighestAll(zoneHi) else Double.NaN;
plot BearZoneLo = if !isBullish and showZones and bn >= scanStart then HighestAll(zoneLo) else Double.NaN;
BullZoneHi.SetDefaultColor(Color.GREEN);
BullZoneLo.SetDefaultColor(Color.GREEN);
BearZoneHi.SetDefaultColor(Color.RED);
BearZoneLo.SetDefaultColor(Color.RED);
AddCloud(BullZoneHi, BullZoneLo, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(BearZoneHi, BearZoneLo, Color.LIGHT_RED, Color.LIGHT_RED);
# --- 78.6% retracement line relative to the SAME swing used for 50/62 zone ---
# Bullish: below the buy zone (measured down from winHigh)
# Bearish: above the sell zone (measured up from winLow)
def Retr78_raw = if isBullish then winHigh - (rng * retrace78Pct) else winLow + (rng * retrace78Pct);
def Retr78_val = HighestAll(Retr78_raw);
plot Retrace78 = if show78Line and showZones and bn >= scanStart then Retr78_val else Double.NaN;
Retrace78.AssignValueColor(if isBullish then Color.GREEN else Color.RED);
Retrace78.SetStyle(Curve.LONG_DASH);
Retrace78.SetLineWeight(2);
Retrace78.HideBubble();
# --- 5) Blue Target Hierarchy ---
plot Target62  = if showTargets and bn >= scanStart then HighestAll(T62_val) else Double.NaN;
plot Target78  = if showTargets and bn >= scanStart then HighestAll(T78_val) else Double.NaN;
plot Target100 = if showTargets and bn >= scanStart then HighestAll(T100_val) else Double.NaN;
Target62.SetDefaultColor(Color.BLUE);  Target62.SetLineWeight(2);
Target78.SetDefaultColor(Color.BLUE);  Target78.SetLineWeight(3);
Target100.SetDefaultColor(Color.BLUE); Target100.SetLineWeight(4);
Target62.HideBubble();
Target78.HideBubble();
Target100.HideBubble();
# --- NEW 5b) Gann PRICE Levels (45/90/180/360) using swing start/end prices ---
# Bullish anchor = winLow (start of swing)
# Bearish anchor = winHigh (start of swing)
def gannAnchor = if isBullish then winLow else winHigh;
def G45_raw  = if isBullish then gannAnchor + (rng * g45mult)  else gannAnchor - (rng * g45mult);
def G90_raw  = if isBullish then gannAnchor + (rng * g90mult)  else gannAnchor - (rng * g90mult);
def G180_raw = if isBullish then gannAnchor + (rng * g180mult) else gannAnchor - (rng * g180mult);
def G360_raw = if isBullish then gannAnchor + (rng * g360mult) else gannAnchor - (rng * g360mult);
def G45_val  = HighestAll(G45_raw);
def G90_val  = HighestAll(G90_raw);
def G180_val = HighestAll(G180_raw);
def G360_val = HighestAll(G360_raw);
plot Gann45  = if showGannPrice and bn >= scanStart then G45_val  else Double.NaN;
plot Gann90  = if showGannPrice and bn >= scanStart then G90_val  else Double.NaN;
plot Gann180 = if showGannPrice and bn >= scanStart then G180_val else Double.NaN;
plot Gann360 = if showGannPrice and bn >= scanStart then G360_val else Double.NaN;
# --- CHANGE: Gann PRICE projections RED + dashed (instead of gray) ---
Gann45.SetDefaultColor(Color.RED);
Gann90.SetDefaultColor(Color.RED);
Gann180.SetDefaultColor(Color.RED);
Gann360.SetDefaultColor(Color.RED);
Gann45.SetStyle(Curve.SHORT_DASH);
Gann90.SetStyle(Curve.SHORT_DASH);
Gann180.SetStyle(Curve.MEDIUM_DASH);
Gann360.SetStyle(Curve.LONG_DASH);
Gann45.SetLineWeight(1);
Gann90.SetLineWeight(1);
Gann180.SetLineWeight(2);
Gann360.SetLineWeight(3);
Gann45.HideBubble();
Gann90.HideBubble();
Gann180.HideBubble();
Gann360.HideBubble();
# --- 6) Black Gann Time Series ---
def off45  = Round(barsBack * pct45, 0);
def off90  = Round(barsBack * pct90, 0);
def off180 = Round(barsBack * pct180, 0);
def turnAnchor = if isBullish then hiBar else loBar;
AddVerticalLine(showTimeTurns and bn == turnAnchor + off45,  "T+" + off45,  Color.BLACK);
AddVerticalLine(showTimeTurns and bn == turnAnchor + off90,  "T+" + off90,  Color.BLACK);
AddVerticalLine(showTimeTurns and bn == turnAnchor + off180, "T+" + off180, Color.BLACK);
# --- 7) Live Counter & Labels ---
def liveCount = if bn >= scanStart then bn - scanStart else 0;
AddLabel(showLabels, if isBullish then "BULLISH SETUP" else "BEARISH SETUP", if isBullish then Color.GREEN else Color.RED);
AddLabel(showZones, (if isBullish then "BUY ZONE: " else "SELL ZONE: ") + Round(zoneLo, 2) + " - " + Round(zoneHi, 2), if isBullish then Color.GREEN else Color.RED);
# Optional label for the 78 line
AddLabel(show78Line, "78.6% RETRACE: " + Round(Retr78_val, 2), if isBullish then Color.GREEN else Color.RED);
# NEW: Gann PRICE labels (match red now)
AddLabel(showGannPriceLabels and showGannPrice,
    "GANN 45/90/180/360: " +
    Round(G45_val, 2) + " | " +
    Round(G90_val, 2) + " | " +
    Round(G180_val, 2) + " | " +
    Round(G360_val, 2),
    Color.RED);
AddLabel(showTargets, "T100 TARGET: " + Round(T100_val, 2), Color.WHITE);
AddLabel(yes, "BARS SINCE START: " + liveCount, Color.BLUE);
